package cc.fine.svc.main.controller.rel;


import cc.fine.svc.common.utils.R;
import cc.fine.svc.main.bean.FineMessage;
import cc.fine.svc.main.bean.dto.VideoInfoDto;
import cc.fine.svc.main.bean.po.RelUserVideoHistory;
import cc.fine.svc.main.service.RelUserVideoHistoryService;
import cc.fine.svc.main.utils.MqUtils;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.replacer.StrReplacer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wyihe
 * @since 2023-10-28
 */
@RestController
@RequestMapping("/history")
@Api(description = "播放记录相关接口管理")
public class RelUserVideoHistoryController {

    @Autowired
    private MqUtils mqUtils;

    @Autowired
    private RelUserVideoHistoryService historyService;

    @ApiOperation("用户添加播放记录（用户播视频时执行，统计播放量和添加历史记录）")
    @PostMapping("/view/{userId}/{videoId}")
    public R view(@PathVariable("userId") String userId,
                  @PathVariable("videoId") String videoId) {
        mqUtils.send(new FineMessage(
                FineMessage.VIDEO_VIEW_HISTORY,
                new RelUserVideoHistory(null, userId, videoId, null)
        ));

        return R.ok();
    }

    @ApiOperation("查看过去n-m天的历史记录（如前0-30天）")
    @GetMapping("/{userId}")
    public R history(@PathVariable String userId,
                     Integer n, Integer m) {
        Date now = new Date();
        Date endTime = DateUtil.offsetDay(now, -n).toJdkDate();
        Date startTime = DateUtil.beginOfDay(DateUtil.offsetDay(now, -m).toJdkDate()).toJdkDate();

        List<Map<String, Object>> res =  historyService.historyVideo(userId, startTime, endTime);
        return R.ok().data("data", res);
    }

}

